using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.Xml;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using CS6238Project2.Banking.Tools;


namespace CS6238Project2.Banking.Communication
{
    /// <summary>
    /// Entity which facilitates in the distribution and usage of a shared key
    /// between two parties; an issuer and a subject.
    /// </summary>
    [Serializable]
    [XmlRoot(IsNullable=true)]
    public class Certificate
    {
        private int acctNumber;
        private string issuer;
        private string subject;
        private DateTime timestamp;
        private XmlDocument transaction;
        private Token token;

        /// <summary>
        /// Constructor for a certificate assigned to particular parties.
        /// </summary>
        /// <param name="i">The issuer of the new certificate</param>
        /// <param name="s">The subject of the new certificate</param>
        public Certificate(string i, string s)
        {
            issuer = i;
            subject = s;
            token = new Token();
            transaction = new XmlDocument();
        }

        /// <summary>
        /// Default certificate is instantiated with a timestamp.
        /// </summary>
        public Certificate()
        {
        }


        public Certificate(Certificate cert)
        {
            this.acctNumber = cert.acctNumber;
            this.issuer = cert.issuer;
            this.subject = cert.subject;
            this.timestamp = cert.timestamp;
            this.transaction = cert.transaction;
            this.token = cert.token;
        }

        /// <summary>
        /// Transaction stored encrypted.  DO NOT USE
        /// WHEN THIS CERTIFICATE HAS BEEN ENCRYPTED.
        /// It will fail.  Use AuthorizedTransaction
        /// </summary>
        [XmlElement(IsNullable=true)]
        public XmlDocument Transaction
        {
            get{ return transaction; }
            set{ transaction =  value; }
        }

        /// <summary>
        /// Gets the transaction that's been encrypted
        /// </summary>
        /// <returns></returns>
        public Transaction d_Transaction()
        { 
            XmlDocument tempDoc = transaction.Clone() as XmlDocument;
            return (Transaction)Tools.Encryption.ObjectifyXml
                (tempDoc, typeof(Transaction), token);
        }


        public void e_Transaction(Transaction transact)
        {
            transaction = Tools.Encryption.Encrypt(transact, token);
        }

        

        /// <summary>
        /// The name of the issuer of the Certificate
        /// </summary>
        public string Issuer
        {
            get { return issuer; }
            set { issuer = value; }
        }

        /// <summary>
        /// The name of the subject the certificate is issused to.
        /// </summary>
        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        /// <summary>
        /// An object which contains cryptographic data for a certificate
        /// </summary>
        [XmlElement(IsNullable=true)]
        public Token Token
        {
            get { return token; }
            set { token = value; }
        }

        /// <summary>
        /// A temporary timestamp for all tickets but the master ticket.
        /// </summary>
        public DateTime TimeStamp
        {
            get{return timestamp; }

            set { timestamp = value; }
        }


    }
}
